
package com.bryanprogramming.zend.logic;

import com.bryanprogramming.zend.logic.powerups.PowerUp;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

/**
 *
 * @author charlesbryan
 */
public class Player implements Iterable<PowerUp>{

    private int goldCoins;
    private ArrayList<PowerUp> powerUps;

    public Player(int coins) {
        this.goldCoins = coins;
        powerUps = new ArrayList<>();

    }
    
    public void addPowerUp(PowerUp pu) {
        if(pu.getCost() > this.goldCoins) {
            throw new IllegalArgumentException("Not enough coins " + pu.getCost());
        }
        removeCoins(pu.getCost());
        this.powerUps.add(pu);
    }
    
    public void removePowerUp(PowerUp pu) {
        this.powerUps.remove(pu);
    }

    /**
     * @return the goldCoins
     */
    public int getGoldCoins() {
        return goldCoins;
    }

    public void addCoins(int coins) {
        int old = this.goldCoins;
        this.goldCoins += coins;
        mPcs.firePropertyChange(Properties.COINS_AMOUNT_CHANGED, old, this.goldCoins);
    }

    public void removeCoins(int coins) {
        if(coins > this.goldCoins) {
            throw new IllegalArgumentException("Not enough coins " + coins);
        }
        int old = this.goldCoins;
        this.goldCoins -= coins;
        mPcs.firePropertyChange(Properties.COINS_AMOUNT_CHANGED, old, this.goldCoins);
    }
    
    public boolean hasEnoughCoins(int coins) {
        return this.goldCoins > coins;
    }
    
    public int getNumberOfPowerUps() {
        return this.powerUps.size();
    }
    
    @Override
    public Iterator<PowerUp> iterator() {
        Collections.sort(powerUps);
        return powerUps.iterator();
    }    
    
    // <editor-fold defaultstate="collapsed" desc="Property Change Support methods and fields">
    //Proerty Change Listener Support code Begin
    private PropertyChangeSupport mPcs = new PropertyChangeSupport(this);

    /**
     * Add a PropertyChangeListener to the listener list.
     *
     * @see
     * java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
     * @param listener The PropertyChangeListener to be added
     */
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        mPcs.addPropertyChangeListener(listener);
    }

    /**
     * Remove a PropertyChangeListener from the listener list.
     *
     * @see
     * java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
     * @param listener The PropertyChangeListener to be removed
     */
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        mPcs.removePropertyChangeListener(listener);
    }

    /**
     * This class holds the list of constants that represent the Property Names
     * used by the PropertySuppoertManager.
     */
    public class Properties {

        public static final String COINS_AMOUNT_CHANGED = "coinsChanged";

    }    
     // </editor-fold>
}
